package com.example.bobo.yuejian322.Fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


import com.example.bobo.yuejian322.Activity.RegisterActivity;
import com.example.bobo.yuejian322.Interfa.LoginSeverice;
import com.example.bobo.yuejian322.Activity.MainActivity;
import com.example.bobo.yuejian322.R;
import com.example.bobo.yuejian322.base.BaseFragment;
import com.example.bobo.yuejian322.bean.Login;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static android.app.Activity.RESULT_CANCELED;
import static android.app.Activity.RESULT_OK;


public class LoginFragment extends BaseFragment {

    private EditText username, userpass;
    private Button LoginButton;
    private TextView regester;
    private  String userName;
    private String  user_name;
    public LoginFragment() {
        // Required empty public constructor
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        username = (EditText) view.findViewById(R.id.name);
        userpass = (EditText) view.findViewById(R.id.pass);
        LoginButton = (Button) view.findViewById(R.id.Login);
        regester = (TextView) view.findViewById(R.id.regrster);
        setonClick();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_login, container, false);
    }


    //监听事件处理
    public void setonClick() {

        //登录事件操作
        LoginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl("http://172.24.10.175/workout/api.php/")
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();

                String strname = username.getText().toString();
                String strpass = userpass.getText().toString();

                LoginSeverice loginService = retrofit.create(LoginSeverice.class);

                Call<Login> userLogincall = loginService.userLogin(strname, strpass);
                userLogincall.enqueue(new Callback<Login>() {
                    @Override
                    public void onResponse(Call<Login> call, Response<Login> response) {
                        Login login = response.body();

                        if (login.getError() == null) {
                            //显示主界面
                            Toast.makeText(getActivity(), "登录成功", Toast.LENGTH_SHORT).show();
                            MainActivity mainActivity = (MainActivity) getActivity();
                            if (mainActivity != null) {
                                mainActivity.replaceLoginFragment();
                                mainActivity.setSavedUserId(login.getSessionid());
                                setUserId(login.getSessionid());
                            }
                        } else {
                            Toast.makeText(getActivity(), "用户名及密码错误", Toast.LENGTH_SHORT).show();
                            username.setText("");
                            userpass.setText("");

                        }
                    }

                    @Override
                    public void onFailure(Call<Login> call, Throwable t) {

                    }

                });


            }
        });


        //注册页面跳转
        regester.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(getActivity(), RegisterActivity.class);
                startActivityForResult(intent,1);
            }
        });
    }

    //获取注册页面得返回值；
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 1:
                switch (resultCode) {
                    case RESULT_CANCELED:
                        break;
                    case RESULT_OK:
                       userName = data.getStringExtra(user_name);
                        username.setText(userName);
                        break;
                }
                break;
            default:
                break;
        }


    }


}